var app = getApp();
var that
Page({
  data: { 
    imgUrls: [
      '../../images/card_img1.png',
      '../../images/card_img2.png', 
      '../../images/card_img3.png',
      '../../images/card_img4.png'
    ], 
    lists:[],
    indicatorDots: true,
    autoplay: false,
    interval: 5000, 
    duration: 1000,
    current: 1,
    hid_img:false,
    cart_agree:'', //购买储值卡协议
    f:'',//从哪里跳到此界面，支付成功是否刷新上一界面，b代表要刷新,暂时未用
    r_pic:'../../images/jian_r2.png',
    l_pic: '../../images/jian_l2.png',
  },
  afterChange: function (e) {
    e.currentTarget.offsetTop = -20;
    that.setData({ current: e.detail.current})
    if (e.detail.current == 0) {
       that.setData({ l_pic: "../../images/jian_l.png" })
    } else { that.setData({ l_pic: "../../images/jian_l2.png" }) }
    if (e.detail.current == (that.data.lists.length-1)){ 
       that.setData({ r_pic: "../../images/jian_r.png" }) 
    } else { that.setData({ r_pic: "../../images/jian_r2.png" }) }
  },
  onLoad: function (options) {
    that=this;
    console.log(options);
    if (options.f){ that.setData({ f: options.f}) }
    var openid = wx.getStorageSync('openid');
    app.req(app.local_url + "do=member&p=recharge&op=level_list", { "od":openid}, function (res) {
      // console.log(res)
      if (res.status == "success") {
        for (var i = 0; i < res.lists.length;i++){
          res.lists[i].phone = that.data.imgUrls[(i+1)%4];
          res.lists[i].checked=0;
          res.lists[i].charge2 = parseFloat(res.lists[i].charge) + parseFloat(res.lists[i].added);
        } 
        that.setData({ lists: res.lists });
      } else if (res.status == "error") {
        app.toast_up(res.mess)//还未添加猫咪提示
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.onLoad(options)
          }
        })
      }
    })
  },
  setDefault:function(e){
    // console.log(e.currentTarget.dataset.idx);
    if(that.data.lists[e.currentTarget.dataset.idx].checked==1){
      that.data.lists[e.currentTarget.dataset.idx].checked =0
    }else{
      that.data.lists[e.currentTarget.dataset.idx].checked = 1
    }
    that.setData({ lists: that.data.lists});
  },
  onReady: function () {
  
  },
  onShow: function () {
    app.ispoint = 0;
    that.setData({ cart_agree: app.hotelData.house_notict.cancel_booking })//买卡须知
    // console.log(that.data.cart_agree)
  },
  buy_cart_agree:function(){ //点击购买协议
    this.setData({
      showModal: true
    })
  },
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  buy:function(e){
    var cart_id = that.data.lists[that.data.current].id;
    var shop_id = wx.getStorageSync('shop_id');
    var openid = wx.getStorageSync('openid');
    if (that.data.lists[that.data.current].checked == 0) {
      app.toast_up("未同意购买协议");
      return
    }
    // console.log('kdk' + app.ispoint)
    if (app.ispoint == 1) { return; } else { app.ispoint = 1; }//弹出支付框后就不允许执行了
    app.req("buy_membercart", { "openid": openid, "cart_id": cart_id,'shop_id':shop_id}, function (res) {
     
      if (res.status == "success") {
         that.wx_pay(res.mess)
      } else if (res.status == "error") {
        app.ispoint = 0;
        app.toast(res.mess)
      } else {
        app.ispoint = 0;
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.buy(e)
          }
        })
      }
    })
  },
  wx_pay: function (pay_msg) {
    wx.requestPayment(
      {
        'timeStamp': pay_msg.timeStamp,
        'nonceStr': pay_msg.nonceStr,
        'package': pay_msg.package,
        'signType': pay_msg.signType,
        'paySign': pay_msg.paySign,
        'success': function (re) {
          that.success();
        },
        'fail': function (re) {
          app.ispoint = 0;
          app.toast_up("支付失败！")
        },
        'complete': function (res) { }
      })
  },
  success: function () {
    var openid = wx.getStorageSync('openid');
    app.toast("支付成功！")
    app.get_msg_rr();   //充值成功后刷新人人商城的信息 
    app.get_p_msg(openid)   //刷新个人信息
    setTimeout(function () { wx.navigateBack({ delta: 1 }) }, 2000)
  },
  img1: function (e) {
    // this.setData({ hid_img: !that.data.hid_img })
    if (that.data.lists[e.currentTarget.dataset.idx].checked == 1) {
      that.data.lists[e.currentTarget.dataset.idx].checked = 0
    } else {
      that.data.lists[e.currentTarget.dataset.idx].checked = 1
    }
    that.setData({ lists: that.data.lists });
  },
})